Tool Tips
UI Behavior (Real Project Context)β
Tooltips display additional information on hover, focus, or click. They are used to improve usability without cluttering the UI.
Common real-world usages:
- Info icons next to form fields
- Validation hints
- Help text on buttons
- Icon-only actions
Tooltips can be native HTML or custom JavaScript components.
Types of Tooltips Youβll Encounterβ
- Native HTML tooltip β uses
titleattribute - Custom tooltip (div/span) β rendered on hover
- Framework tooltips β Bootstrap, Material UI, etc.
Native HTML Tooltip (title attribute)β
HTMLβ
<button title="Save changes">Save</button>
Selenium Handlingβ
No hover is required β read the attribute directly.
WebElement saveBtn = driver.findElement(By.tagName("button"));
String tooltip = saveBtn.getAttribute("title");
Assert.assertEquals(tooltip, "Save changes");
Custom Tooltip (Hover-Based)β
Typical HTMLβ
<span class="info-icon">i</span>
<div class="tooltip">Enter valid email</div>
Tooltip appears only after hover.
Hovering Using Actions Classβ
WebElement infoIcon = driver.findElement(By.cssSelector(".info-icon"));
Actions actions = new Actions(driver);
actions.moveToElement(infoIcon).perform();
Waiting for Tooltip Visibilityβ
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement tooltip = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".tooltip"))
);
Assert.assertEquals(tooltip.getText(), "Enter valid email");
Handling Framework Tooltips (Bootstrap Example)β
Bootstrap tooltips often use data-* attributes.
<button data-toggle="tooltip" title="Delete record">Delete</button>
Selenium strategy remains the same β read title or hover if dynamically rendered.
Tooltip on Disabled Elementsβ
Disabled elements donβt receive hover.
Workaroundβ
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].dispatchEvent(new MouseEvent('mouseover'));", infoIcon);
Common Mistakes ββ
- Trying to locate tooltip before hover
- Using
Thread.sleepinstead of explicit wait - Assuming all tooltips use
title - Ignoring disabled-element behavior
- Not validating tooltip text
Best Practices β β
- Identify tooltip type before automation
- Prefer attribute validation when possible
- Use
Actionsfor hover-based tooltips - Always wait for tooltip visibility
- Validate tooltip content explicitly
Interview Notes π―β
Q: How do you handle tooltips in Selenium?
A: By reading the title attribute or hovering and validating tooltip text.
Q: Which class is used for hover actions?
A: Actions class.
Q: Can you hover on disabled elements?
A: Not directly; JavaScript workaround may be required.
Real-Project Tip π‘β
Tooltips are often missed in automation but frequently tested in accessibility and UX audits.
Summaryβ
- Tooltips can be native or custom
- Hover-based tooltips need Actions
- Explicit waits improve stability
- Content validation is essential